/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import org.hibernate.Query;
import org.hibernate.Session;
import until.HibernateUtil;
import java.util.List;
import pojo.Binhluan;
import pojo.Danhsachsanpham;
import pojo.Donhang;
import pojo.Thanhvien;

/**
 *
 * @author Alvis
 */
public class ThanhVienDao {

    Session session;

    public ThanhVienDao() {
        session = HibernateUtil.getSessionFactory().openSession();
    }

    public void Insert(Thanhvien thanhvien) {
        session.getTransaction().begin();
        session.save(thanhvien);
        session.flush();
        session.getTransaction().commit();


    }

    public void Update(Thanhvien thanhvien, String username) {
        session.getTransaction().begin();
        Thanhvien thvien = (Thanhvien) session.get(Thanhvien.class, username);
        thvien.setPassword(thanhvien.getPassword());
        thvien.setNguoidung(thanhvien.getNguoidung());
        thvien.setLoaithanhvien(thanhvien.getLoaithanhvien());
        thvien.setTinhtrangbannick(thanhvien.getTinhtrangbannick());

        session.update(thvien);
        session.flush();
        session.getTransaction().commit();

    }

    public Thanhvien getMember(String username) {

        session.getTransaction().begin();
        String sql = "from Thanhvien where username=?";
        Query query = session.createQuery(sql);

        query.setString(0, username);
        Thanhvien result = (Thanhvien) query.uniqueResult();
        Date a= result.getNguoidung().getNgaysinh();
        session.flush();
        session.getTransaction().commit();
        return result;

    }

    public void Delete(String username) {
        session.getTransaction().begin();
        String sql = "from Donhang where username=?";
        Query query = session.createQuery(sql);
        query.setString(0,username);
        List result = query.list();
        Donhang[] listDonHang = new Donhang[result.size()];
        result.toArray(listDonHang);
        for(int i=0;i<listDonHang.length;i++)
        {
            
         
            sql = "from Danhsachsanpham where madonhang=?";
            query = session.createQuery(sql);
            query.setInteger(0,listDonHang[i].getMadonhang());
            result = query.list();
            Danhsachsanpham[] listDanhSachSanPham = new Danhsachsanpham[result.size()];
            result.toArray(listDanhSachSanPham);
            for(int j=0;j<listDanhSachSanPham.length;j++)
            {
                session.delete(session.get(Danhsachsanpham.class,listDanhSachSanPham[j].getId()));
            }
            session.delete(session.get(Donhang.class,listDonHang[i].getMadonhang()));
            
        }
        sql = "from Binhluan where username=?";
        query = session.createQuery(sql);
        query.setString(0,username);
        result = query.list();
        Binhluan[] listBinhLuan = new Binhluan[result.size()];
        result.toArray(listBinhLuan);
        for(int i=0;i<listBinhLuan.length;i++)
        {
            session.delete(session.get(Binhluan.class,listBinhLuan[i].getMabinhluan()));
        }
        session.delete(session.get(Thanhvien.class, username));
        session.flush();
        session.getTransaction().commit();
    }

    public Thanhvien[] getAllMember() {
        session.getTransaction().begin();
        String sql = "from Thanhvien";
        Query query = session.createQuery(sql);
        List result = query.list();
        Thanhvien[] listThanhVien = new Thanhvien[result.size()];
        result.toArray(listThanhVien);
        return listThanhVien;
    }
    public Thanhvien[] searchMember(String username) {
        session.getTransaction().begin();
        String sql = "from Thanhvien where username like ?";
        Query query = session.createQuery(sql);
        query.setString(0,"%" +username+"%");
        List result = query.list();
        Thanhvien[] listThanhVien = new Thanhvien[result.size()];
        result.toArray(listThanhVien);
        return listThanhVien;
    }
}
